package yxy.game.pm2.module.playtype;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.PropertyData;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;

import java.util.HashMap;

//战力计算中间缓存
public class PlayTypeModule extends PlayerModule {

    private PropertyData weapon  = new PropertyData();//神器的属性

    private PropertyData avatar = new PropertyData();//形象

    private PropertyData title = new PropertyData(); //称号

    private HashMap<Integer, PropertyData> union = new HashMap<Integer, PropertyData>(); //联盟  key 角色定位

    private HashMap<Integer, PropertyData> JadeSeal = new HashMap<Integer, PropertyData>(); //玉玺  key 阵营

    private PropertyData therion = new PropertyData();  //神兽

    private HashMap<Integer,PropertyData> PickedTroop = new HashMap<Integer, PropertyData>(); //兵符槽 key 阵营

    private HashMap<Integer,PropertyData> PickedTroopSlot = new HashMap<Integer, PropertyData>(); //兵符词条属性  key 目标对象 1.前排   2.后排  3.全体

    private PropertyData playerCommand = new PropertyData(); //主公统御加成

    private PropertyData fourSeal = new PropertyData(); //四象归一

//    private PropertyData captureStrategic = new PropertyData(); //斩关夺隘(旧)
    private PropertyData passBarrierStars = new PropertyData(); //斩关夺隘

    //===============================================================================================================================

    public PropertyData getPassBarrierStars() {
        return passBarrierStars;
    }

    public void setPassBarrierStars(PropertyData passBarrierStars) {
        this.passBarrierStars = passBarrierStars;
    }


//    public PropertyData getCaptureStrategic() {
//        return captureStrategic;
//    }
//
//    public void setCaptureStrategic(PropertyData captureStrategic) {
//        this.captureStrategic = captureStrategic;
//    }

    public PropertyData getWeapon() {
        return weapon;
    }

    public void setWeapon(PropertyData weapon) {
        this.weapon = weapon;
    }

    public PropertyData getAvatar() {
        return avatar;
    }

    public void setAvatar(PropertyData avatar) {
        this.avatar = avatar;
    }

    public PropertyData getTitle() {
        return title;
    }

    public void setTitle(PropertyData title) {
        this.title = title;
    }

    public HashMap<Integer, PropertyData> getUnion() {
        return union;
    }

    public void setUnion(HashMap<Integer, PropertyData> union) {
        this.union = union;
    }

    public HashMap<Integer, PropertyData> getJadeSeal() {
        return JadeSeal;
    }

    public void setJadeSeal(HashMap<Integer, PropertyData> jadeSeal) {
        JadeSeal = jadeSeal;
    }

    public PropertyData getTherion() {
        return therion;
    }

    public void setTherion(PropertyData therion) {
        this.therion = therion;
    }

    public HashMap<Integer, PropertyData> getPickedTroop() {
        return PickedTroop;
    }

    public void setPickedTroop(HashMap<Integer, PropertyData> pickedTroop) {
        PickedTroop = pickedTroop;
    }

    public HashMap<Integer, PropertyData> getPickedTroopSlot() {
        return PickedTroopSlot;
    }

    public void setPickedTroopSlot(HashMap<Integer, PropertyData> pickedTroopSlot) {
        PickedTroopSlot = pickedTroopSlot;
    }

    public PropertyData getPlayerCommand() {
        return playerCommand;
    }

    public void setPlayerCommand(PropertyData playerCommand) {
        this.playerCommand = playerCommand;
    }

    public PropertyData getFourSeal() {
        return fourSeal;
    }

    public void setFourSeal(PropertyData fourSeal) {
        this.fourSeal = fourSeal;
    }

    // func===========================================================================


    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:PLAYTYPE", playerCode);
    }

    public void set() {
        try {
            String key = key(getPlayerCode());
            setv(key, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public PlayTypeModule get(String playerCode) {
        try {
            PlayTypeModule module = getv(key(playerCode));
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), PlayTypeModule.class);
                if(module != null){module.set();}//把数据同步到redis
            }
            if (module == null) {
                module = create(playerCode);
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //第一次初始化
    static public PlayTypeModule create(String playerCode) {
        PlayTypeModule module = new PlayTypeModule();
        module.setPlayerCode(playerCode);
        PlayTypeHandle.saveJadeSeal(playerCode,module);
        PlayTypeHandle.savePickedGodBeast(playerCode,module);
        PlayTypeHandle.saveUnion(playerCode,module);
        PlayTypeHandle.saveTitle(playerCode,module);
        PlayTypeHandle.saveWeapon(playerCode,module);
        PlayTypeHandle.savePickedTroop(playerCode,module);
        PlayTypeHandle.savePlayerCommand(playerCode,module);
        PlayTypeHandle.saveFourSeal(playerCode,module);
        module.set();
        return module;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                PlayTypeModule module = PlayTypeModule.get(playerCode);
                MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }

}
